package com.fzu.normalservice.controller;


import com.fzu.commonutil.ImResult;
import com.fzu.commonutil.entity.Company;
import com.fzu.commonutil.entity.UserVO;
import com.fzu.commonutil.mapper.CompanyMapper;
import com.fzu.commonutil.util.CodeEnum;
import com.fzu.commonutil.util.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzu.commonutil.entity.User;
import com.fzu.normalservice.param.LoginParam;
import com.fzu.normalservice.param.RegisterParam;
import com.fzu.normalservice.param.WxRegisterParam;
import com.fzu.normalservice.service.UserService;
import com.fzu.commonutil.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {
    private final UserService userService;
    private final CompanyMapper companyMapper;

    /**
     * 登录
     * @param param 账号信息
     * @return 登录信息
     */
    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginParam param) {
        return R.ok().data(userService.login(param));
    }

    /**
     * 微信用户注册
     * @param param 注册信息
     * @return token
     */
    @PostMapping("/wx-register")
    public R wxRegister(@RequestBody @Valid WxRegisterParam param) {
        return R.ok().data(userService.wxRegister(param));
    }

    /**
     * 微信用户登录
     * @param code 微信临时票据
     * @return token
     */
    @PostMapping("/wx-login/{code}")
    public R wxLogin(@PathVariable("code") String code) {
        return R.ok().data(userService.wxLogin(code));
    }


    /**
     * 注册
     * @param param 注册信息
     * @return 无
     */
    @PostMapping("/register")
    public R register(@RequestBody @Valid RegisterParam param) {
        userService.register(param);
        return R.ok();
    }

    /**
     * 解密令牌获取用户信息
     *
     * @param token
     * @return
     */
    @GetMapping("/decode")
    public User decode(@RequestParam String token) {
        return JwtUtil.getUser(token);
    }

    /*
     * @Description: 查询除了自己以外所有的用户
     * @Param: [id]
     * @return: java.util.List<com.fzu.commonutil.entity.User>
     **/
    @GetMapping("/getAllUser/{id}")
    public List<User> getAllUser(@PathVariable String id) {
        User curUser = userService.getById(id);
        Integer role = curUser.getRole();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(User::getId, id);
        Map<Integer, String> companyMap = new HashMap<>();
        if (CodeEnum.CONSUMER.getCode().equals(role)) {
            LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
            companyWrapper.eq(Company::getState, CodeEnum.ENABLE.getCode());
            List<Company> companies = companyMapper.selectList(companyWrapper);
            companyMap = companies.stream().collect(Collectors.toMap(Company::getUserId, Company::getName));
            Set<Integer> companyManagerIds = companies.stream().map(Company::getUserId).collect(Collectors.toSet());
            wrapper.in(User::getId, companyManagerIds);
        }
        List<User> list = userService.list(wrapper);
        if (CodeEnum.CONSUMER.getCode().equals(role)) {
            for (User user : list) {
                user.setNickName(companyMap.get(user.getId()));
            }
        }
        return list;
    }

    @GetMapping("/getUser/{id}")
    private User getUserById(@PathVariable String id) {
        log.info("访问getUserById方法");
        User user = userService.getById(id);
        return user;
    }
    @GetMapping("/test")
    public R test() {
        return R.ok().data("test");
    }

    /**
     * 查询所有列表
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping("/list")
    public R userList(User user,
                      @RequestParam(required = false, defaultValue = "10") Integer pageNum,
                      @RequestParam(required = false, defaultValue = "1") Integer pageSize){
        return R.ok().data(userService.list(user, pageNum, pageSize));
    }

    /**
     * 增加用户
     * @param user
     * @param token
     * @return
     */
    @PostMapping
    public R add(@RequestBody User user,@RequestHeader String token){
        User user1 = JwtUtil.getUser(token);
        user.setId(user1.getId());
        userService.save(user);
        return R.ok();
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping
    public R update(@RequestBody User user){
        userService.updateUser(user);
        return R.ok();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Integer id){
        userService.deleteUser(id);
        return R.ok();
    }


    /**
     * 根据id查询地址数据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        return R.ok().data(user);
    }

    @PostMapping("/token")
    public ImResult<UserVO> login(@RequestParam("userToken") String userToken) {
        log.info("执行token方法");
        return userService.token(userToken);
    }
}

